package com.cgc999.host.base;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.trello.rxlifecycle3.components.support.RxFragment;

public abstract class BaseLazyFragment extends RxFragment {
    public static final byte INIT_NOT = 0x00;  //未加载
    public static final byte INIT_ING = 0x01;  //加载中
    public static final byte INIT_FINISH = 0x02;  //加载完成
    protected byte initState = INIT_NOT;

    private boolean isCanShowing = true;
    private boolean isFirstResume = true;
    @Override
    public void onResume() {
        super.onResume();
        isCanShowing = isFirstResume || isVisible();
        isFirstResume = false;
        onVisibleChanged(isVisibleOnScreen());
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        isCanShowing = !hidden;
        onVisibleChanged(isVisibleOnScreen());
    }
    protected void onVisibleChanged(boolean isVisible) {
    }
    @Override
    public void onStop() {
        super.onStop();
        isCanShowing = false;
        onVisibleChanged(isVisibleOnScreen());
    }
    // 判断可见性，对手动显示与PagerAdapter方式均有效，且跟随父fragment可见性状态
    public boolean isVisibleOnScreen() {
        if (isCanShowing && getUserVisibleHint() && isVisible()) {
            if (getParentFragment() == null) {
                return true;
            }
            if (getParentFragment() instanceof BaseLazyFragment) {
                return ((BaseLazyFragment) getParentFragment()).isVisibleOnScreen();
            } else {
                return getParentFragment().isVisible();
            }
        }
        return false;
    }

    public abstract void reFreshData();


    //第一次可见加载
    protected void onFirstVisiableLoad() {
    }

    //当可见
    protected void onVisiable() {
    }

    //当不可见
    protected void onInvisiable() {
    }

    private Handler mPriHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:  //可见
                    onVisiable();
                    break;
                case 2: //不可见
                    onInvisiable();
                    break;
                case 3: //第一次加载
                    if (initState == INIT_NOT) {
                        initState = INIT_ING;
                        onFirstVisiableLoad();
                        initState = INIT_FINISH;
                        mPriHandler.sendEmptyMessage(1);
                    }
                    break;
            }
        }
    };

    /**
     * 方法只在ViewPager中的Fragment中才执行
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isCanShowing = isVisibleToUser;
        onVisibleChanged(isVisibleOnScreen());
        if (isVisibleToUser) {
            if (initState == INIT_FINISH) {
                mPriHandler.sendEmptyMessage(1);
            } else if (initState == INIT_NOT) {
                mPriHandler.sendEmptyMessageDelayed(3, 100);//延迟0.1秒加载
            }
        } else {
            if (initState == INIT_FINISH) {
                mPriHandler.sendEmptyMessage(2);
            } else {
                mPriHandler.removeMessages(3);
            }
        }
    }
}
